package com.qf.lja.dao;

import com.qf.lja.enity.Item;
import com.qf.lja.enity.Product;
import com.qf.lja.util.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ItemDao {
    public void addItem(List<Item> items) {
        String sql = "insert into item(o_id,p_id,i_count,i_num) values(?,?,?,?)";

        JDBCUtil jdbcUtil = new JDBCUtil();
        for (Item item : items) {
            jdbcUtil.update(sql, item.getO_id(), item.getP_id(), item.getI_count(), item.getI_num());
        }
        jdbcUtil.closeConnection();
    }

    public List<Item> findItemByOrderId(String orderId) {
        //item和product查询
        String sql = "select * from product p join item i on p.product_id=i.p_id where i.o_id = ?";
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet rs = jdbcUtil.query(sql, orderId);
        List<Item> items = new ArrayList<>();
        try {
            while (rs.next()) {
                Item item = new Item();
                item.setI_id(rs.getInt("i_id"));
                item.setO_id(rs.getString("o_id"));
                item.setP_id(rs.getInt("p_id"));
                item.setI_count(rs.getDouble("i_count"));
                item.setI_num(rs.getInt("i_num"));
                item.setProduct(convertToProduct(rs));
                items.add(item);
            }
            return items;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }


    private Product convertToProduct(ResultSet rs) {
        try {
            Product product = new Product();
            product.setProductId(rs.getInt("product_id"));
            product.setProductName(rs.getString("product_name"));
            product.setPrice(rs.getDouble("price"));
            product.setLaunchDate(rs.getString("launch_date")); // Assuming launch_date is stored as String
            product.setImageUrl(rs.getString("image_url"));
            product.setPopularityIndex(rs.getInt("popularity_index"));
            product.setStatus(rs.getInt("status"));
            product.setInventory(rs.getInt("inventory"));
            product.setDescription(rs.getString("description"));
            product.setModifiedAt(rs.getString("modified_at"));
            return product;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Item> findByOrderId(String orderId) {
        String sql = "select * from item where o_id = ?";
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet rs = jdbcUtil.query(sql, orderId);
        List<Item> items = new ArrayList<>();
        try {
            while (rs.next()) {
                Item item = new Item();
                item.setI_id(rs.getInt("i_id"));
                item.setO_id(rs.getString("o_id"));
                item.setP_id(rs.getInt("p_id"));
                item.setI_count(rs.getDouble("i_count"));
                item.setI_num(rs.getInt("i_num"));
                items.add(item);
            }
            return items;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
